/* eslint-disable keyword-spacing,indent */
// +----------------------------------------------------------------------
// | CmsWing [ 网站内容管理框架 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2115 http://www.cmswing.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: arterli <arterli@qq.com>
// +----------------------------------------------------------------------

module.exports = class extends think.cmswing.admin {
  constructor(ctx) {
    super(ctx); // 调用父级的 constructor 方法，并把 ctx 传递进去
    // 其他额外的操作
    this.tactive = 'release';
  }
  /**
   * index action
   * @return {Promise} []
   */
  async indexAction() {
    const map = {};
    map['g.zzjg'] = ['!=', null];
    map['g.appmobile'] = null;
    const q = this.get('q');
   if (!think.isEmpty(q)) {
     map['z.zzjg'] = ['like', '%' + q + '%'];
   }

   const data = await this.model('gongxufabu').alias('g').where(map).join({
    table: 'zzjg',
    join: 'inner', // join 方式，有 left, right, inner 3 种方式
    as: 'z', // 表别名
    on: ['g.zzjg', 'z.id'] // ON 条件
    }).page(this.get('page') || 1, 20).order('g.id DESC')
    .field('g.id,g.faburen,g.xinxileixing,g.shangpingleibie,g.diqu,g.shangpingshuliang,g.jiage,g.status,g.mokuaibiaoshi,z.zzjg')
    .countSelect();
    const html = this.pagination(data);
    this.assign('pagerData', html); // 分页展示使用
    // console.log(data.data);
    this.active = 'admin/neiqin/index';
    this.assign('list', data.data);
    this.meta_title = '内勤查询页面';
    return this.display();
  }
  // 内勤发布列表
  async listAction() {
    const user = await this.session('userInfo');
    console.log('user=' + user);
    const status = this.get('status');
    const map = {};
    map.zzjg = user.zzjg;
    if (!think.isEmpty(status)) {
      map.status = status;
      this.assign('status', status);
    }
    const q = this.get('q');
   if (!think.isEmpty(q)) {
     map['shangpingleibie|diqu'] = ['like', '%' + q + '%'];
   }
    const memberIds = await this.model('member').where({groupid: ['=', 11]}).getField('id');
    map.faburen = ['IN', memberIds];
    // this.config("db.nums_per_page",20)
    const data = await this.model('gongxufabu').where(map).page(this.get('page') || 1, 20).order('id DESC').countSelect();
    const html = this.pagination(data);
    this.assign('pagerData', html); // 分页展示使用
    // console.log(data.data);
    this.active = 'admin/neiqin/list';
    this.assign('list', data.data);
    this.meta_title = '内勤管理页面';
    return this.display();
  }

  /**
   * 业务内勤派单业务员
   */
  async zhipaiAction() {
    if (this.isPost) {
      console.log('+++++++++');
      const id = this.post('id');
      const phone = this.post('phone');
      const resfor = await this.model('api/member').where({mobile: phone, groupid: 12}).count();
      if(resfor == 0) { return this.fail('此号码无对应业务员') }
      const paidanshijian = new Date().toLocaleString();
      const res = await this.model('gongxufabu').where({id: id}).update({appmobile: phone, status: 3, paidanshijian: paidanshijian});
      return this.success({name: '指定业务员成功，请提醒相关业务员接收派单！！'});
    } else {
      const id = this.get('id');
      this.assign('id', id);
      const yuangong = await this.model('api/member').where({groupid: 12, ygbh: ['NOTLIKE', '']}).select();
      const res = [];
      for (let index = 0; index < yuangong.length; index++) {
        const element = yuangong[index];
        const resIndex = res.findIndex((item) => { return item.text === element.gsmc });
        if (resIndex == -1) {
          res.push({
            text: element.gsmc,
            nodes: [
              {
                text: element.real_name + '-' + element.mobile,
                mobile: element.mobile
              }]
          });
        } else {
          res[resIndex].nodes.push({
            text: element.real_name + '-' + element.mobile,
            mobile: element.mobile
          });
        }
      }
      this.assign('treeviewData', JSON.stringify(res));
      this.meta_title = '业务员分配';
      return this.display();
    }
  }

  /**
   * 直接释放发布信息
   */
  async shifangAction() {
    if (this.isPost) {
      const id = this.post('id');
      const shifang_remark = this.post('shifang_remark');
      const shifangshijian = new Date().toLocaleString();
      const shifang = await this.model('gongxufabu').where({id: id}).update({status: 5, shifang_remark: shifang_remark, shifangshijian: shifangshijian});
      if (shifang) {
        return this.success({name: '信息释放成功！'});
      } else {
        return this.fail('操作失败！');
      }
    }else {
      const id = this.get('id');
      this.assign('id', id);
      this.meta_title = '释放订单';
      return this.display();
    }
  }

  /**
     * 查看订单
     * @returns {*}
     */
  async seeAction() {
    const id = this.get('id');
     console.log(id);
    this.meta_title = '查看发布详情';
    // 获取发布信息
    const gongxufabu = await this.model('gongxufabu').find(id);
    this.assign('gongxufabu', gongxufabu);
    const url = JSON.parse(gongxufabu.tupian);
    this.assign('url', url);
    // 购买人信息
    const user = await this.model('member').where({id: gongxufabu.faburen}).find();
    this.assign('user', user);
    return this.display();
  }
};
